/**
 * Date:2022年1月6日 上午10:19:09
 * Copyright (c) 2022, www.bwbroad.com All Rights Reserved
 */

package com.example.common.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: TODO <br/>
 * Date:2022年1月6日 上午10:19:09 <br/>
 * 
 * @author xuejianxin
 */
public class JavaBeanUtils {
	/**
	 * 下划线转驼峰式（String）
	 * 
	 * @param lineString
	 * @param smallCamel true-返回小驼峰,false-大驼峰式
	 * @return
	 */
	public static String underlineToCamel(String lineString, boolean smallCamel) {
		if (null == lineString || "".equals(lineString))
			return lineString;
		StringBuffer sb = new StringBuffer();
		Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?"); // 字母、数字、下划线
		Matcher matcher = pattern.matcher(lineString);
		while (matcher.find()) {
			String word = matcher.group();
			sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
					: Character.toUpperCase(word.charAt(0)));
			int index = word.lastIndexOf("_");
			if (index > 0) {
				sb.append(word.substring(1, index).toLowerCase());
			} else {
				sb.append(word.substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线转驼峰式（Map）
	 * 
	 * @param map
	 * @return newMap
	 */
	public static Map<String, Object> underlineToCamel(Map<String, Object> map) {
		Map<String, Object> newMap = new HashMap<String, Object>();
		Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
		Entry<String, Object> entry;
		while (iterator.hasNext()) {
			entry = iterator.next();
			if (null != entry.getKey() && entry.getKey() instanceof String) {
				newMap.put(underlineToCamel(entry.getKey(), true), entry.getValue());
			}
			iterator.remove();
		}
		return newMap;
	}
}
